package com.cly.controller;

import com.cly.base.controller.BaseController;
import com.cly.dto.FileChunkDto;
import com.cly.entity.File;
import com.cly.result.JsonRequest;
import com.cly.service.IFileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

import java.util.List;

/**
 * 文件控制器
 *
 * @author by 春风能解释
 * <p>
 * 2024/8/31
 */
@RestController
@RequestMapping("/file")
@Tag(name = "文件控制类", description = "文件操作访问接口")
public class FileController extends BaseController<IFileService, File> {

    /**
     * 分页查询文件
     *
     * @param current 页码
     * @param size    页长
     * @param file    条件
     * @return JSON
     */
    @GetMapping
    @Operation(summary = "分页查询文件")
    public JsonRequest<List<File>> pageFile(
            @Parameter(description = "页码", required = true)
            @RequestParam Integer current,
            @Parameter(description = "页长", required = true)
            @RequestParam Integer size,
            @Parameter(description = "文件表条件", required = true)
            File file) {
        return baseService.pageFile(current, size, file);
    }

    /**
     * 文件上传并返回ID
     *
     * @param file 文件
     * @return JSON
     */
    @PostMapping("/upload")
    @Operation(summary = "文件上传并返回ID")
    public JsonRequest<Long> uploadFile(
            @Parameter(description = "文件", required = true)
            @RequestPart @RequestParam("file") MultipartFile file) {
        return baseService.uploadFile(file);
    }

    /**
     * 根据ID查询文件信息
     *
     * @param id 文件ID
     * @return JSON
     */
    @GetMapping("/getOne")
    @Operation(summary = "根据ID查询文件信息")
    public JsonRequest<File> getOne(
            @Parameter(description = "文件ID", required = true)
            @RequestParam Long id) {
        return baseService.getOne(id);
    }

    /**
     * 添加文件备注
     *
     * @param file 数据
     * @return JSON
     */
    @PostMapping("/addNote")
    @Operation(summary = "添加文件备注")
    public JsonRequest<Integer> addNote(
            @Parameter(description = "文件", required = true)
            @RequestBody File file) {
        return baseService.addNote(file);
    }

    /**
     * 检查文件是否已上传
     *
     * @param fileChunk 文件分片传输对象
     * @return JSON
     */
    @PostMapping("/check")
    @Operation(summary = "检查文件是否已上传")
    public JsonRequest<Long> checkFile(
            @Parameter(description = "文件分片传输对象", required = true)
            @RequestPart FileChunkDto fileChunk) {
        return baseService.checkFile(fileChunk);
    }

    /**
     * 文件分片上传
     *
     * @param fileChunk 文件分片传输对象
     * @return JSON
     */
    @PostMapping("/chunk")
    @Operation(summary = "文件分片上传")
    public JsonRequest<List<Long>> uploadFileChunk(
            @Parameter(description = "文件分片传输对象", required = true)
            FileChunkDto fileChunk) {
        return baseService.uploadFileChunk(fileChunk);
    }


    /**
     * 合并文件
     *
     * @param fileChunk 文件分片传输对象
     * @return JSON
     */
    @PostMapping("/merge")
    @Operation(summary = "合并文件")
    public JsonRequest<Long> mergeFile(
            @Parameter(description = "文件分片传输对象", required = true)
            FileChunkDto fileChunk) {
        return baseService.mergeFile(fileChunk);
    }

    /**
     * 根据ID删除文件
     *
     * @param ids 文件ID
     * @return JSON
     */
    @DeleteMapping("/delete")
    @Operation(summary = "根据ID删除文件")
    public JsonRequest<Integer> deleteFileById(
            @Parameter(description = "文件ID", required = true)
            @RequestBody List<Long> ids) {
        return baseService.deleteFileById(ids);
    }

    /**
     * 根据ID文件下载
     *
     * @param id 文件路径
     * @return ResponseEntity
     */
    @GetMapping("/download")
    @Operation(summary = "根据ID文件下载")
    public ResponseEntity<StreamingResponseBody> showFile(
            @Parameter(description = "文件ID", required = true)
            @RequestParam Long id) {
        return baseService.showFile(id);
    }

    /**
     * zip下载文件
     *
     * @param ids 文件ID
     * @return JSON
     */
    @PostMapping("/zipDownload")
    @Operation(summary = "zip下载文件")
    public JsonRequest<Long> zipDownloadFiles(
            @Parameter(description = "文件ID", required = true)
            @RequestBody List<Long> ids) {
        return baseService.zipDownloadFiles(ids);
    }

    /**
     * 分片下载文件
     *
     * @param id    文件ID
     * @param range 请求头中的Range
     * @return ResponseEntity
     */
    @GetMapping("/downloadChunk")
    @Operation(summary = "分片下载文件")
    public ResponseEntity<StreamingResponseBody> chunkDownload(
            @Parameter(description = "文件ID", required = true)
            @RequestParam Long id,
            @Parameter(description = "请求头中的Range")
            @RequestHeader(value = "Range", required = false) String range) {
        return baseService.chunkDownload(id, range);
    }

    /**
     * 判断是否为图片
     *
     * @param id 文件ID
     * @return JSON
     */
    @GetMapping("/isImg")
    @Operation(summary = "判断是否为图片")
    public JsonRequest<Boolean> isImage(
            @Parameter(description = "文件ID", required = true)
            @RequestParam Long id) {
        return baseService.isImage(id);
    }

}
